<?php 

/**
 * OperatorController
 *
 * @author yukio
 * @version $Id$
 */

require_once 'Zend/Controller/Action.php';
require_once 'Zend/Validate/EmailAddress.php';
require_once 'Zend/Mail.php';
require_once 'Zend/Auth.php';

require_once 'Okute/Util/Data.php';
require_once 'Okute/Exception.php';

require_once APPLICATION_PATH . '/models/AccountModel.php';
require_once APPLICATION_PATH . '/models/RequesterModel.php';
require_once APPLICATION_PATH . '/models/PartyModel.php';
require_once APPLICATION_PATH . '/models/ResetModel.php';
require_once APPLICATION_PATH . '/models/AclModel.php';

class OperatorController extends Zend_Controller_Action
{
    public function init()
    {
        $this->_session = new Zend_Session_Namespace('okute');

        $this->_session->requester = null;
        $this->_session->shelter   = null;
        $this->_session->party     = null;

        $this->_util = new Okute_Util_Data($this->view);
        $this->_config = Zend_Registry::get('config');
        $this->_acl = Zend_Registry::get('acl');
        $this->_redirector = Zend_Registry::get('redirector');
        
        $this->_session->operator = 'operator';
        $this->view->base = $this->getRequest()->getBaseUrl();
    }
    
    public function indexAction()
    {
        if (!$this->_acl->isAllowed($this->_session->role, AclModel::RES_OPERATOR_MENU)) {
            throw new Okute_Exception('Access Control Error');
        }
    }

    /**
     * 支援希望者一覧処理
     *
     * @return void
     */
    public function requesterAction()
    {
        if (!$this->_acl->isAllowed($this->_session->role, AclModel::RES_REQUESTER_LIST)) {
            throw new Okute_Exception('Access Control Error');
        }
        
        // パラメータ群のセット
        $format = $this->_session->format;
        $count = $this->_config->display->request_count_per_page->$format;
        $page = $this->getRequest()->getParam('page',1);
        
         // ページネーション処理
        $requesterModel = new RequesterModel();
        $select = $requesterModel->select();
        $select->setIntegrityCheck(false);
        $select->from('mst_requester', 'mst_requester.*');
        $select->joinLeft('mst_shelter',
                          'mst_requester.shelter_id = mst_shelter.shelter_id',
                          array('prefecture','area','city','district','place_name'=>'name'));
        $select->joinLeft('mst_account',
                          'mst_requester.requester_id = mst_account.account_id',
                          array('account_type','status','ac_update_datetime'=>'update_datetime'));
        $select->order('status ASC');
        $select->order('ac_update_datetime DESC');

        Zend_Paginator::setDefaultScrollingStyle('Sliding');
        $requesters = Zend_Paginator::factory($select);
        $requesters->setCurrentPageNumber($page);
        $requesters->setItemCountPerPage($count);
        
        // 不正なページが入力されたときの対策としてpaginatorが保持するページで上書きする
        $page = $requesters->getCurrentPageNumber();
        
        $request = $this->getRequest();
        $controller = $request->getControllerName();
        $action = $request->getActionName();
        
        $this->view->base = $request->getBaseUrl();
        $this->view->link = '/'.$controller.'/'.$action;
        
        $this->view->requesters = $requesters;
        $this->view->page = $requesters->getPages();
    }
    
    /**
     * 団体一覧処理
     *
     * @return void
     */
    public function partyAction()
    {
        if (!$this->_acl->isAllowed($this->_session->role, AclModel::RES_PARTY_LIST)) {
            throw new Okute_Exception('Access Control Error');
        }
        
        // パラメータ群のセット
        $format = $this->_session->format;
        $count = $this->_config->display->request_count_per_page->$format;
        $page = $this->getRequest()->getParam('page',1);
        
        // ページネーション処理
        $partyModel = new PartyModel();
        $select = $partyModel->select();
        $select->setIntegrityCheck(false);
        $select->from('mst_party', 'mst_party.*');
        $select->joinLeft('mst_account',
                          'mst_party.party_id = mst_account.account_id',
                          array('account_type','status','ac_update_datetime'=>'update_datetime'));
        $select->order('status ASC');
        $select->order('ac_update_datetime DESC');
        
        Zend_Paginator::setDefaultScrollingStyle('Sliding');
        $parties = Zend_Paginator::factory($select);
        $parties->setCurrentPageNumber($page);
        $parties->setItemCountPerPage($count);
        
        // 不正なページが入力されたときの対策としてpaginatorが保持するページで上書きする
        $page = $parties->getCurrentPageNumber();
        
        $request = $this->getRequest();
        $controller = $request->getControllerName();
        $action = $request->getActionName();
        
        $this->view->base = $request->getBaseUrl();
        $this->view->link = '/'.$controller.'/'.$action;
        
        $this->view->parties = $parties;
        $this->view->page = $parties->getPages();
        $this->view->partyType = array(1 => '支援団体',2 => '輸送団体');
        
    }
    
    /**
     * アカウントステータスの有効化
     *
     * @return void
     */
    public function accountAction()
    {
        if (!$this->_acl->isAllowed($this->_session->role, AclModel::RES_ACCOUNT_ENABLE)) {
            throw new Okute_Exception('Access Control Error');
        }
        
        $id = $this->_getParam('id');
        $status = $this->_getParam('status');
        $back = $this->_getParam('back');
        
        $accountModel = new accountModel();
        $where = $accountModel->getAdapter()->quoteInto('account_id = ?', $id);
        $accountModel->update(array('status' => $status, 'update_datetime' => 'NOW()'), $where);
        $this->_forward($back);
    }

}